<?php

namespace App\Http\Controllers\Miniprograme;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Car;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use function AlibabaCloud\Client\json;

class SubscribeController extends ComController
{
    use ResponseData;
    //获取模板消息列表
    public function template_list(){
        return $this->miniProgram->subscribe_message->getTemplates();
    }
    //已经送达
    public function send_arrive($order,$message='已送达'){
        $data = [
            'template_id' =>config('wechat.mini_program.default.order_status'),
            'touser' => $order->openid,
            'page' => 'pages/user/order/order',
            'data' =>[
                'thing1'=>[
                    'value'=>'订单消息'
                ],
                'phrase3'=>[
                    'value'=>$message
                ],
                'thing4'=>[
                    'value'=>'如有疑问请联系：400-0579-686'
                ]
            ]
        ];
        $data = $this->miniProgram->subscribe_message->send($data);
        Log::info('下单通知'.json_encode($data));
    }
    //确认接单
    public function confirm_order($openid,$message='已送达'){
        $data = [
            'template_id' =>config('wechat.mini_program.default.confirm_order'),
            'touser' => $openid,
            'page' => 'pages/index/index',
            'data' =>[
                'name1'=>[
                    'value'=>'送车员',
                ],
                'phone_number2'=>[
                    'value'=>'联系电话',
                ],
                'time3'=>[
                    'value'=>'送达时间',
                ],
                'thing4'=>[
                    'value'=>'取车地点',
                ],

                'thing5'=>[
                    'value'=>'如有疑问请联系：400-0579-686'
                ]
            ]
        ];
        $data = $this->miniProgram->subscribe_message->send($data);
    }
    //发货通知
    public function send_template($order){
        $data = [
            'template_id' =>config('wechat.mini_program.default.order_status'),//config('wechat.mini_program.default.order_send'),//config('wechat.mini_program.default.'.$request->template_id), // 所需下发的订阅模板id
            'touser' => $order->openid,
            'page' => 'pages/user/order/order',
            'data' =>[
                'thing1'=>[
                    'value'=>'订单消息'
                ],
                'phrase3'=>[
                    'value'=>'送车中'
                ],
                'thing4'=>[
                    'value'=>date('Y-m-d H:i:s')
                ]
            ]
        ];
        $data = $this->miniProgram->subscribe_message->send($data);
    }
    //用户下单
    public function create_order($order){
        $data = [
            'template_id' =>config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.'.$request->template_id), // 所需下发的订阅模板id
            'touser' => $order->openid,
            'page' => 'pages/user/order/order',
            'data' =>[
                'thing1'=>[
                    'value'=>'创建订单'
                ],
                'phrase3'=>[
                    'value'=>'待分配'
                ],
                'thing4'=>[
                    'value'=>'您好，你已经成功下单'
                ]
            ]
        ];
        $data = $this->miniProgram->subscribe_message->send($data);
        Log::info('下单通知'.json_encode($data));
    }
    //付款成功
    public function TradeSuccess($order){
        $data = [
            'template_id' =>config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.'.$request->template_id), // 所需下发的订阅模板id
            'touser' => $order->openid,
            'page' => 'pages/user/order/order',
            'data' =>[
                'thing1'=>[
                    'value'=>'付款成功'
                ],
                'phrase3'=>[
                    'value'=>'待分配'
                ],
                'thing4'=>[
                    'value'=>'【邦拓租车】订单确认：您预定的××车辆已确认，稍后为您送出，客服店长电话：×××'
                ]
            ]
        ];
        $data = $this->miniProgram->subscribe_message->send($data);
    }

    /**
     * @param $openid 消息接收者
     * @param $title 标题
     * @param $status 订单状态
     * @param $mark 描述
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 通用 订单状态提醒
     */
    public function OrderStatus($openid,$title,$status,$mark){
        $data = [
            'template_id' =>config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.'.$request->template_id), // 所需下发的订阅模板id
            'touser' => $openid,
            'page' => 'pages/user/order/order',
            'data' =>[
                'thing1'=>[
                    'value'=>$title
                ],
                'phrase3'=>[
                    'value'=>$status
                ],
                'thing4'=>[
                    'value'=>$mark
                ]
            ]
        ];
        $this->miniProgram->subscribe_message->send($data);
    }
    /**
     * @param $openid 消息接收者
     * @param $title 标题
     * @param $status 订单状态
     * @param $mark 描述
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 通用 订单状态提醒
     */
    public function manager($openid,$order){
        $car_name = Car::query()->where('id',$order->car_series_id)->value('name');
        $arr_address = json_decode($order->position,true);
        $address = $arr_address['address'];
        $data = [
            'template_id' =>config('wechat.mini_program.manager.create_order'),//config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.'.$request->template_id), // 所需下发的订阅模板id
            'touser' => $openid,
            'page' => 'pages/admin/home/home',
            'data' =>[
                'thing1'=>[
                    'value'=>$car_name
                ],
                'thing2'=>[
                    'value'=>$address
                ],
                'time3'=>[
                    'value'=>date("Y/m/d H:i:s",strtotime($order->pre_fetch_time))
                ],
                'time4'=>[
                    'value'=>date("Y/m/d H:i:s",strtotime($order->pre_return_time))
                ],
                'thing6'=>[
                    'value'=>$order->wx_name
                ],

            ]
        ];
        $res = $this->manager->subscribe_message->send($data);
        info('发送数据:'.json_encode($data));
        Log::info('下单模板消息:'.json_encode($res));
    }
//    换车系确认通知
    public function ChangeCarNotify($order,$old,$new,$content){
        $data = [
            'template_id' =>config('wechat.mini_program.default.change_serices'),//config('wechat.mini_program.default.create_order'),//config('wechat.mini_program.default.'.$request->template_id), // 所需下发的订阅模板id
            'touser' => $order->openid,
            //pages/orderDetail/index?id=12&status=1&sn=2023021912
            'page' => 'pages/orderDetail/index?id='.$order->id.'&status='.$order->status.'&sn='.$order->order_sn,
            'data' =>[
                'car_number1'=>[
                    'value'=>$old
                ],
                'car_number2'=>[
                    'value'=>$new
                ],
                'thing5'=>[
                    'value'=>$content
                ]
            ]
        ];
        $this->miniProgram->subscribe_message->send($data);
    }


}
